package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.EventsNumBiz;
import com.egao.orderB.entity.EventsNum;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-05-25 15:42:33
 * @version ：1.0
 */
@Repository
public class EventsNumBizImpl extends BaseDao implements EventsNumBiz {

	@Override
	public JsonResult list(PageResult2<T> page, EventsNum eventsNum, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<EventsNum> rm = new BeanPropertyRowMapper<EventsNum>(EventsNum.class);
		StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.user_id");
        sql.append(",e.module,e.nums,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_events_num e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id ");
        sql.append(" where e.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(eventsNum)){
			if(Utils.isNotEmpty(eventsNum.getModule())){
				sql.append(" and instr(e.module,:module)>0 ");
				map.put("module", eventsNum.getModule());
			}
			if(Utils.isNotEmpty(eventsNum.getSort())&&Utils.isNotEmpty(eventsNum.getOrder())){
                sql.append(" order by e."+eventsNum.getSort()+" "+eventsNum.getOrder());
            } else {
                sql.append(" order by e.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<EventsNum> list(EventsNum eventsNum, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<EventsNum> rm = new BeanPropertyRowMapper<EventsNum>(EventsNum.class);
        StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.user_id");
        sql.append(",e.module,e.nums,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_events_num e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id ");
        sql.append(" where e.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(eventsNum)){
			if(Utils.isNotEmpty(eventsNum.getModule())){
				sql.append(" and instr(e.module,:module)>0 ");
				map.put("module", eventsNum.getModule());
			}
            if(Utils.isNotEmpty(eventsNum.getSort())&&Utils.isNotEmpty(eventsNum.getOrder())){
				sql.append(" order by e."+eventsNum.getSort()+" "+eventsNum.getOrder());
			} else {
				sql.append(" order by e.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public EventsNum getById(EventsNum eventsNum) {
		RowMapper<EventsNum> rm = new BeanPropertyRowMapper<EventsNum>(EventsNum.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.user_id");
        sql.append(",e.module,e.nums,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_events_num e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 and e.id=:id ");
		map.put("id", eventsNum.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}


	@Override
	public Integer getByUserId(EventsNum eventsNum) {
		try {
			RowMapper<EventsNum> rm = new BeanPropertyRowMapper<EventsNum>(EventsNum.class);
			Map<String, String> map  = new HashMap<String, String>();
			StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.user_id");
			sql.append(",e.module,e.nums,e.create_time,e.create_user");
			sql.append(",e.update_time,e.update_user");
			sql.append(",suser.username createName ");
			sql.append(" from chf_events_num e ");
			sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
			sql.append(" where e.deleted=0  ");
			sql.append(" and e.module=:module and e.user_id=:user_id ");
			map.put("module", eventsNum.getModule());
			map.put("user_id", eventsNum.getUserId()+"");
			getMapParameter(sql, map);
			return jdbcTemplate.queryForObject(sql.toString(), map, rm).getNums();
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public boolean save(EventsNum eventsNum, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_events_num (");
        sql.append("organization_id,user_id,module,nums");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:user_id,:module,:nums");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("user_id", eventsNum.getUserId()==null?null:eventsNum.getUserId()+"");
        map.put("module", eventsNum.getModule());
        map.put("nums", eventsNum.getNums()==null?null:eventsNum.getNums()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(EventsNum eventsNum, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_events_num set ");
        sql.append("user_id=:user_id,module=:module,nums=:nums ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("user_id", eventsNum.getUserId()==null?null:eventsNum.getUserId()+"");
        map.put("module", eventsNum.getModule());
        map.put("nums", eventsNum.getNums()==null?null:eventsNum.getNums()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", eventsNum.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateNum(EventsNum eventsNum, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_events_num set ");
		sql.append("nums=:nums,update_time=now(),update_user=:update_user where   ");
		sql.append("user_id=:user_id and module=:module ");
        map.put("user_id", eventsNum.getUserId()+"");
        map.put("module", eventsNum.getModule());
        map.put("nums", eventsNum.getNums()+"");
		map.put("update_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(EventsNum eventsNum, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(eventsNum.getId().length()>0){
			for(String ID : eventsNum.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_events_num set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
